package com.gpoint.rdp.door.zoneCTL.controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.gpoint.rdp.base.core.util.string.StringUtil;
import com.gpoint.rdp.base.core.entity.ResultMessage;
import com.gpoint.rdp.base.framework.page.PageList;
import com.gpoint.rdp.base.framework.page.PageResult;
import com.gpoint.rdp.api.base.query.QueryFilter;
import com.gpoint.rdp.base.web.controller.GenericController;
import com.gpoint.rdp.base.web.json.PageJson;
import com.gpoint.rdp.base.web.util.RequestUtil;
import com.gpoint.rdp.door.zoneCTL.service.DoorZoneService;
import com.gpoint.rdp.door.zoneCTL.service.DoorZoneQueryService;
import com.gpoint.rdp.door.zoneCTL.persistence.entity.DoorZonePo;
import com.gpoint.rdp.door.zoneCTL.persistence.entity.DoorControllerPo;
import com.gpoint.rdp.base.core.util.JacksonUtil;

import java.util.List;

/**
 * TB_DOOR_ZONE 控制类
 *
 *<pre> 
 * 开发公司：苏州东大金点信息技术有限公司
 * 开发人员：唐红锋
 * 邮箱地址：55872655@qq.com
 * 创建时间：2019-09-26 18:33:32
 *</pre>
 */
@Controller
@RequestMapping("/rdp/zoneCTL/doorZone/")
public class DoorZoneController extends GenericController{
	@Resource
	private DoorZoneService doorZoneService;
	@Resource
	private DoorZoneQueryService doorZoneQueryService;
	
	/**
	 * 【TB_DOOR_ZONE】列表(分页条件查询)数据
	 *
	 * @param request
	 * @param reponse
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("listJson")
	public @ResponseBody PageJson listJson(HttpServletRequest request,HttpServletResponse reponse) throws Exception{
		QueryFilter queryFilter = getQuerFilter(request);
		try{
			String listData = doorZoneQueryService.query(queryFilter);
			PageList<DoorZonePo> doorZoneList = null;
			if(JacksonUtil.isJsonObject(listData)){
				List<DoorZonePo> list = DoorZonePo.fromJsonArrayString(JacksonUtil.getString(listData, "data"));
				PageResult pageResult = PageResult.fromJson(JacksonUtil.getString(listData, "pageResult"));
				doorZoneList = new PageList<DoorZonePo>(list, pageResult);
			}
			
			return new PageJson(doorZoneList);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			return new PageJson();
		}
	}
	
	/**
	 * 编辑【TB_DOOR_ZONE】信息页面
	 *
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("edit")
	public ModelAndView edit(HttpServletRequest request,HttpServletResponse response) throws Exception{
		String preUrl= RequestUtil.getPrePage(request);
		String id=RequestUtil.getString(request, "id");
		DoorZonePo doorZone=null;
		if(StringUtil.isNotEmpty(id)){
			String data = null;
			data = doorZoneQueryService.loadCascade(id);
			doorZone = DoorZonePo.fromJsonString(data);
		}

		return getAutoView().addObject("doorZone", doorZone).addObject("returnUrl", preUrl);
	}
	
	/**
	 * 编辑【TB_DOOR_ZONE】信息页面
	 *
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("flowEdit")
	public ModelAndView flowEdit(HttpServletRequest request,HttpServletResponse response) throws Exception{
		String preUrl= RequestUtil.getPrePage(request);
		String id=RequestUtil.getString(request, "id");
		DoorZonePo doorZone=null;
		if(StringUtil.isNotEmpty(id)){
			String data = null;
			data = doorZoneQueryService.loadCascade(id);
			doorZone = DoorZonePo.fromJsonString(data);
		}
		return getAutoView().addObject("doorZone", doorZone).addObject("returnUrl", preUrl);
	}
	
	/**
	 * 【TB_DOOR_ZONE】明细页面
	 *
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("get")
	public ModelAndView get(HttpServletRequest request,HttpServletResponse response) throws Exception{
		String preUrl= RequestUtil.getPrePage(request);
		String id=RequestUtil.getString(request, "id");
		DoorZonePo doorZone=null;
		if(StringUtil.isNotEmpty(id)){
			String data = null;
			data = doorZoneQueryService.loadCascade(id);
			doorZone = DoorZonePo.fromJsonString(data);
		}
		return getAutoView().addObject("doorZone", doorZone).addObject("returnUrl", preUrl);
	}
	
	/** 
	 * 保存【TB_DOOR_ZONE】信息
	 *
	 * @param request
	 * @param response
	 * @param  doorZone
	 * @throws Exception
	 */
	@RequestMapping("save")
	public void save(HttpServletRequest request,HttpServletResponse response) throws Exception{
		ResultMessage message=null;
		try {
			String json = RequestUtil.getString(request, "json");
			doorZoneService.saveCascade(json);
			message=new ResultMessage(ResultMessage.SUCCESS, "保存TB_DOOR_ZONE成功");
		} catch (Exception e) {
			message=new ResultMessage(ResultMessage.FAIL, "对TB_DOOR_ZONE操作失败,"+e.getMessage());
			logger.error("对TB_DOOR_ZONE操作失败，" + e.getMessage(),e);
		}
		writeResultMessage(response.getWriter(), message);
	}
	
	/**
	 *  批量删除【TB_DOOR_ZONE】记录
	 *
	 * @param request
	 * @param response
	 * @throws Exception
	 */
	@RequestMapping("remove")
	public void remove(HttpServletRequest request,HttpServletResponse response) throws Exception{
		ResultMessage message=null;
		try {
			//获得待删除的id
			String[] ids=RequestUtil.getStringAryByStr(request, "id");
			doorZoneService.deleteByIdsCascade(ids);
			message=new ResultMessage(ResultMessage.SUCCESS, "删除TB_DOOR_ZONE成功");
		} catch (Exception e) {
			message=new ResultMessage(ResultMessage.FAIL, "删除TB_DOOR_ZONE失败，" + e.getMessage());
			logger.error("删除TB_DOOR_ZONE失败，" + e.getMessage(),e);
		}
		writeResultMessage(response.getWriter(), message);
	}
	
	 @RequestMapping("list2")
	 public ModelAndView list(HttpServletRequest request,HttpServletResponse response) throws Exception{
		 ModelAndView mv= new ModelAndView("/rdp/zone/zTree.jsp") ;
	  return mv;
	  
	 }
}